﻿package app.view.components.sections.russia 
{
	import app.model.RemoteProxy;
	import app.view.ApplicationMediator;
	import app.view.components.buttons.SelectableButton;
	import app.view.components.guiDima.PopupHeadDBase;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 3.1 ИНТЕРАКТИВНАЯ КАРТА
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class RussiaMapDBase extends LabeledSectionBase
	{
		
		public var area:Sprite;
		public var btn_01:SelectableButton;
		public var btn_02:SelectableButton;
		public var btn_03:SelectableButton;
		public var btn_04:SelectableButton;
		public var btn_05:SelectableButton;
		public var btn_06:SelectableButton;
		private var _btn:/*SelectableButton*/Array;
		
		private var _current:SelectableButton;
		private var _dataItems:Array;
		private var _currentPopups:PopupHeadDBase;
		private var _itemsPopups:/*PopupHeadDBase*/Array;
		
		public function RussiaMapDBase() 
		{
			super();
			
		}
		
		override protected function init():void
		{
			super.init();
			
			
			area.mouseChildren = false;
			area.mouseEnabled = true;
			
			_btn = [btn_01, btn_02, btn_03, btn_04, btn_05, btn_06]
			
			
		}
		
		private function setEvents():void
		{
			for (var i:int = 0; i < _btn.length; i++) 
			{
				_btn[i].id = i;
				_btn[i].addEventListener(MouseEvent.CLICK,onClickBtn);
			}
			
			addEventListener(UIEvent.CLICK_MINI_POPUPS, onClickMiniPopups, true);
		}
		
		private function removeEvents():void
		{
			for (var i:int = 0; i < _btn.length; i++) 
			{
				_btn[i].removeEventListener(MouseEvent.CLICK,onClickBtn);
			}
			removeEventListener(UIEvent.CLICK_MINI_POPUPS, onClickMiniPopups, true);
		}
		
		/**
		 * установить выделенную линию
		 */
		public function setSelectedItem(ind:int):void
		{
			if (_current==null) 
			{
				_current = _btn[ind];
				_current.selected = true;
				
				createPopups(ind);
				
			}else
			{
				_current.selected = false;
				_current = _btn[ind];
				_current.selected = true;
				
				createPopups(ind);
			}
			
		}
		
		private function createPopups(ind:int):void
		{
			clearPopupsArray();
			
			//trace('+++',_dataItems[ind].data.length);
			
			for (var i:int = 0; i < _dataItems[ind].data.length; i++) 
			{
				//trace('***',_dataItems[ind].data[i]);
				var item:PopupHeadDBase = new PopupHeadDBase(i);
				addChild(item);
				item.setParamContent(_dataItems[ind].data[i].name, _dataItems[ind].data[i].text, _dataItems[ind].data[i].image);
				
				
				item.x = ApplicationMediator.SCREEN_WIDTH*Number(_dataItems[ind].data[i].posx)/100;
				item.y = ApplicationMediator.SCREEN_HEIGHT*Number(_dataItems[ind].data[i].posy)/100;
				
				/*trace('+++', _dataItems[ind].data[i].posx, _dataItems[ind].data[i].posy);
				trace('***x',item.x , area.x);
				trace('***y',item.y , area.y);*/
				_itemsPopups.push(item);
			}
			
		}
		
		private function clearPopupsArray():void
		{
			// чистим	
			if (_itemsPopups) 
			{
				for (var j:int = 0; j < _itemsPopups.length; j++) 
				{
					_itemsPopups[j].destroy();
					removeChild(_itemsPopups[j]);
				}
				
				var len:int = _itemsPopups.length;
				for (var i:int = 0; i < len; i++) 
				{
					_itemsPopups.pop();
				}
				_currentPopups = null;
			}
			
			_itemsPopups = new Array();
		}
		
		/**
		 * установить выделенную линию
		 */
		public function setSelectedItemPopups(ind:int):void
		{
			if (_currentPopups==null) 
			{
				_currentPopups = _itemsPopups[ind];
				_currentPopups.selected = true;
			}else
			{
				_currentPopups.selected = false;
				_currentPopups = _itemsPopups[ind];
				_currentPopups.selected = true;
			}
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			clearPopupsArray();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			////trace('data!!!+++', _data.data);
			
			setParamHead(HeadMarkerBase.BGD_GREEN);
			
			
			var nameBtn:Array = new Array();
			
			_dataItems = new Array();
			
			for each(var object:* in _data.data.cat)
			{
				var str:String = '';
				for each(var object1:* in object.head)
				{
					str = String(object1.text).substr(1);
				}
				//trace('head', str);
				nameBtn.push(str);
				
				
				var arr:Array = new Array();
				
				for each(var objectRegion:* in object.region)
				{
					var objRegion:Object = new Object();
					objRegion.posx = objectRegion.text[1];
					objRegion.posy = objectRegion.text[2];
					objRegion.name = objectRegion.text[3];
					objRegion.text = objectRegion.text[4];
					objRegion.image = (objectRegion.image[1].image_path != '') ? RemoteProxy.DOMEN + objectRegion.image[1].image_path : '';
					
					
					//trace('image',objectRegion.image[1].image_path);
					//trace('text1',objectRegion.text[1]);
					//trace('text2',objectRegion.text[2]);
					//trace('text3',objectRegion.text[3]);
					//trace('text4',objectRegion.text[4]);
					
					
					//for (var name:String in objectRegion.image[1]) 
					//{
						//trace('name=',name);
					//}
					trace('---------------',objRegion.posx,objRegion.posy);
					
					arr.push(objRegion);
				}
				
				_dataItems.push({data:arr});
			}
			
			for (var i:int = 0; i < nameBtn.length; i++) 
			{
				_btn[i].text.text = nameBtn[i];
			}
			
			setEvents();
		};
		
		/**
		 * клик по кнопке
		 * @param	e
		 */
		private function onClickBtn(e:MouseEvent):void 
		{
			//trace('id=',SelectableButton(e.target).id);
			
			setSelectedItem(SelectableButton(e.target).id);
			
		}
		
		/**
		 * 
		 * @param	e
		 */
		private function onClickMiniPopups(e:UIEvent):void 
		{
			trace('+++',e.id);
			
			setSelectedItemPopups(e.id);
		}
		
	}

}